package com.test.suanfa;

/**
 * 双指针使用
 * 给定一个有序数据{0,1,2,2,3,3,4}，在当前数组删除重复元素，返回数据长度，有点类似count(distince)操作
 */
public class 删除重复元素 {

    public static void main(String[] args) {
        test1();
        test4();
    }

    public static void test4() {
        int[] nums = {0, 1, 2, 2, 2, 3, 3, 4};
        int i = 0;
        for (int j = 0; j < nums.length; j++) {
            if (nums[i] != nums[j]) {
                i = i + 1;
                nums[i] = nums[j];
            }
        }
        System.out.println(i + 1);
    }

    public static void test3() {
        int[] nums = {0, 1, 2, 2, 2, 3, 3, 4};
        int i = 0;
        for (int j = 1; j < nums.length; j++) {
            if (nums[i] != nums[j]) {
                i = i + 1;
                nums[i] = nums[j];
            }
        }
    }


    public static void test1() {
        int[] nums = {0, 1, 2, 2, 2, 3, 3, 4};
        int i = 0;
        for (int j = 1; j < nums.length; j++) {
            if (nums[j] != nums[i]) {
                i = i + 1;
                nums[i] = nums[j];
            }
        }
        System.out.println(i + 1);
    }
}
